/* 鼠标移动 跟随显示 */
import RangeingToolTip from "./tool/RangingToolTip";
import Draw from "ol/interaction/Draw";
import {layerStyle} from "./tool/style";
import {unByKey} from "ol/Observable";

class FlowTitleTool {
  constructor(map,source,layer) {
    //地图实例
    this.map = map;
    //提示条
    this.toolTip = new RangeingToolTip();
    this.drawTool = null;
    //源
    this.source = source; // 对应 me.all_obj.monitor.data_c
    //图层
    this.layer = layer;// 对应 me.all_obj.monitor.layer
  }
  /** 调置资源等 */
  set(map,source,layer){
    this.map = map;
    this.source = source;
    this.layer = layer;
  }
  setMap(map){this.map=map;this.toolTip.setMap(map);}
  setSource(source){this.source=source;}
  setLayer(layer){this.layer=layer;}

  start(){
    //交互
    this.drawTool = new Draw({
      type: "LineString",
      source: this.source,
      style: this.toolTip.range_style()
    });
    this.map.addInteraction(this.rangingDrawTool);

    let listener=null;
    const that = this;

    this.drawTool.on('drawstart',function(evt) {
      console.log("drawstart.evt: %o",evt);
      that.toolTip.sketch = evt.feature;
      let tooltipCoord = evt.coordinate;
      listener = that.toolTip.sketch.getGeometry().on('change', function(evt1) {
        //i++;
        //console.log("change.evt1: %o",evt1);
        let geom = evt1.target;
        let output = that.toolTip.formatLength(geom);
        tooltipCoord = geom.getLastCoordinate();
        //let pos=tooltipCoord;
        //if (i==1)
        //console.log(pos);
        that.toolTip.measureTooltipElement.innerHTML = output;
        that.toolTip.measureTooltip.setPosition(tooltipCoord);
      });
    });

    this.rangingDrawTool.on('drawend', function(event) {
      event.feature.setStyle(layerStyle);
      event.feature.type = "LineString";
      event.feature.type_id = "LineString";
      event.feature.method="Range";
      that.toolTip.measureTooltipElement.className = 'tooltip tooltip-static remove_div';
      that.toolTip.measureTooltip.setOffset([0, -7]);
      that.toolTip.sketch = null;
      that.toolTip.measureTooltipElement = null;
      that.toolTip.createMeasureTooltip();
      //Observable.unByKey(listener);
      unByKey(listener);
      //console.log(pos);
    });
  }

  finish(){
    const that = this;
    this.source.getFeatures().forEach(function(feature) {
      let method = feature.get('method');
      if (feature.method != null && feature.method == "Range") {
        that.layer.getSource().removeFeature(feature);
      }
    });

    //me.tt.obj.helpTooltipElement.innerHTML="";
    this.map.removeInteraction(this.rangingDrawTool);
    this.rangingDrawTool=null;
    if (this.toolTip.mouseoutEvent)
      //Observable.unByKey(this.toolTip.mouseoutEvent);
      unByKey(this.toolTip.mouseoutEvent);
    if (this.toolTip.pointermoveEvent)
      //Observable.unByKey(this.toolTip.pointermoveEvent);
      unByKey(this.toolTip.pointermoveEvent);
    if (this.toolTip.measureTooltip != null)
      this.map.removeOverlay(this.toolTip.measureTooltip);
    if (this.toolTip.helpTooltip != null)
      this.map.removeOverlay(this.toolTip.helpTooltip);
    const removeDivs = document.getElementsByClassName('remove_div');
    for(var i=0;i < removeDivs.length; i++)
    {
      removeDivs[i].style.visibility = 'hidden';
    }

  }

}
export default FlowTitleTool;
